<!--[if IE]>
<style>
.tilda {
    position: absolute;
}
</style>
<script>
jQuery(document).ready(function($) {
   $(window).scroll(function() {
      $('.tilda').each(function() {
         $(this).css({top: $('body').attr('scrollTop')});
      });
   });
});
</script>
<![endif]-->

<script src="{{=URL(request.application,'static','js/jquery.terminal-0.4.17.min.js')}}"></script>
<link href="{{=URL(request.application,'static','css/jquery.terminal.css')}}" rel="stylesheet"/>

<script>

/*String.prototype.strip = function(char) {
    return this.replace(new RegExp("^" + char + "*"), '').
        replace(new RegExp(char + "*$"), '');
}


$.extend_if_has = function(desc, source, array) {
    for (var i=array.length;i--;) {
        if (typeof source[array[i]] != 'undefined') {
            desc[array[i]] = source[array[i]];
        }
    }
    return desc;
};*/


(function($) {
    $.fn.tilda = function(eval, options) {
        if ($('body').data('tilda')) {
            return $('body').data('tilda').terminal;
        }
        this.addClass('tilda');
        options = options || {};
        eval = eval || function(command, term) {
            term.echo("you don't set eval for tilda");
        };
        var settings = {
            prompt: 'kvasir>',
            name: 'Kvasir',
            height: 500,
            enabled: false,
            greetings: 'Greetings professor Falkin.',
        };
        if (options) {
            $.extend(settings, options);
        }
        this.append('<div class="td" name="mousetrap_off"></div>');
        var self = this;
        self.terminal = this.find('.td').terminal(eval, settings);
        var focus = false;
        $(document.documentElement).keypress(function(e) {
            if (e.which == 96) {
                self.slideToggle('fast');
                self.terminal.set_command('');
                self.terminal.focus(focus = !focus);
                self.terminal.attr({
                    scrollTop: self.terminal.attr("scrollHeight")
                });
            }
        });
        $('body').data('tilda', this);
        /*
        if (this.is(':visible'))
            Mousetrap.pause();
        else
            Mousetrap.unpause();
        */
        this.hide();
        return self;
    };
})(jQuery);

//--------------------------------------------------------------------------
jQuery(document).ready(function($) {

    $('#tilda').tilda(function(entry, terminal) {
        var c = entry.split(' ');
        var command = c[0].toLowerCase();

        if (c.length > 1)
            var optargs = c.slice(1).join(' ');
        else
            var optargs = "";

        // TODO: Could use some parsing code here.. optarg for javascript?

        if (command == "help") {
            terminal.echo('Help:\n\nlist games\nplay [game]\ngo|jump [host rec/ip address]\n');
        } else if (command == "list") {
            if (optargs.split(' ')[0] == "games") {
                terminal.echo('Games\n-----\n\nChess\nCheckers\nYahtzee\nMunchkin\nAir Traffic Controller\nStar Trek\nHunt The Wumpus\nGlobal Thermonuclear War\n');
            }
        } else if (command == "play") {
            terminal.echo('Play with me!');
        } else if (command == "play") {
            if (optargs == "global thermonuclear war") {
                terminal.echo('Wouldn\'t you like to play a nice game of Chess?');
            }
        } else if ((command == "go") || (command == "jump")) {
            terminal.echo('Jumping to host ' + command.substr(3))
            window.location = "{{=URL('hosts', 'detail')}}/" + command.substr(3)
        } else {
            terminal.echo('you typed "' + command + ' ' + optarg + '"');
        }
    });
});

</script>

<div id="tilda"></div>
